import { Controller } from "../../mvc/core/Controller";
import { IPlayerSavvyController } from "./interfaces/IPlayerSavvyController";
import { IView } from "../../mvc/interfaces/IView";
import { GameController } from "../mvc/GameController";
import { IPlayerModule } from "../../../modules/interfaces/IPlayerModule";
import { IPlayerRootSavvyModule } from "../../../modules/interfaces/IPlayerRootSavvyModule";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EGateCommand } from "../../../Message/Command/EGateCommand";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { SGetSavvy } from "../../../Message/Game/SGetSavvy";
import { AcupointData } from "../view/player/acupoint/AcupointData";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SSavvyRate } from "../../../Message/Game/SSavvyRate";
import { SAcupointInfo } from "../../../Message/Game/SAcupointInfo";
type int = number;
//class PlayerSavvyController
    
    export  class PlayerSavvyController extends Controller implements IPlayerSavvyController
    {
       

        protected /* override */ initView(): IView
        {
            if (this._savvyModule == null) 
            {
                this._savvyModule = (GameController.player.view as IPlayerModule).rootSavvyModule;
            }
            return this._savvyModule;
        }

        public get savvyModule(): IPlayerRootSavvyModule
        {
            this.initView();
            return this._savvyModule;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.AcupointOpenSavvyModule, this.onSavvyModuleOpenHandler);
            Dispatcher.addEventListener(EventName.AcupointGetSavvyLevelInfo, this.onGetSavvyLevelInfoHandler);
            Dispatcher.addEventListener(EventName.AcupointGetSavvyLevelSuccess, this.onGetSavvyLevelSuccessHandler);
            Dispatcher.addEventListener(EventName.AcupointUpgradeSavvy, this.onUpgradeSavvyHandler);
            Dispatcher.addEventListener(EventName.AcupointUpgradeSavvySuccess, this.onUpgradeSavvySuccessHandler);
            Dispatcher.addEventListener(EventName.AcupointClearCdTime, this.onClearCdTimeHandler);
            Dispatcher.addEventListener(EventName.AcupointClearCdTimeSuccess, this.onClearCdTimeSuccessHandler);
            Dispatcher.addEventListener(EventName.AcupointGetSavvyExLevelSuccess, this.onGetSavvyExLevelSuccessHandler);
            Dispatcher.addEventListener(EventName.AcupointUpgradeSavvyExSuccess, this.onUpgradeSavvyExSuccessHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateSavvyRate, this.onSavvyRateChangeHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateAcupointInfo, this.onAcupointInfoReciveHandler);
            return;
        }

        /* internal  */onSavvyModuleOpenHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            this.savvyModule.setCurStep(loc1);
            GameProxy.acupoint.getSavvy(loc1);
            this.savvyModule.show(430, -5);
            this.savvyModule.showPanel(1);
            return;
        }

        /* internal  */onGetSavvyLevelInfoHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.acupoint.getSavvyEx(loc1);
            return;
        }

        /* internal  */onGetSavvyLevelSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SGetSavvy;
            this.savvyModule.updateSavvyInfo(loc1);
            return;
        }

        /* internal  */onGetSavvyExLevelSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            var loc2=loc1.count as int;
            var loc3=loc1.rate as int;
            this.savvyModule.updateSavvyCount(loc2, loc3);
            return;
        }

        /* internal  */onUpgradeSavvyExSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            var loc2=loc1.step as int;
            var loc3=loc1.count as int;
            var loc4=loc1.newLevel as int;
            var loc5=loc1.result as Boolean;
            var loc6=loc1.rate as int;
            AcupointData.setSavvyLevel(loc2, loc4);
            this.savvyModule.updateSavvyCount(loc3, loc6);
            if (loc5) 
            {
                this.savvyModule.updateSavvyConfigInfo(loc2);
                MsgManager.showRollTipsMsg(Language.getString(30206));
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(30207));
            }
            return;
        }

        /* internal  */onUpgradeSavvyHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            GameProxy.acupoint.upgradeSavvyEx(loc1);
            return;
        }

        /* internal  */onUpgradeSavvySuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            var loc2=loc1.step as int;
            var loc3=loc1.getSavvyInfo as SGetSavvy;
            var loc4=loc1.newLevel as int;
            var loc5=loc1.result as Boolean;
            AcupointData.setSavvyLevel(loc2, loc4);
            this.savvyModule.upgradeReturn(loc3);
            if (loc5) 
            {
                this.savvyModule.updateSavvyConfigInfo(loc2);
                MsgManager.showRollTipsMsg(Language.getString(30206));
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(30207));
            }
            return;
        }

        /* internal  */onClearCdTimeHandler(arg1: DataEvent): void
        {
            GameProxy.acupoint.clearSavvyCd();
            return;
        }

        /* internal  */onClearCdTimeSuccessHandler(arg1: DataEvent): void
        {
            this.savvyModule.upgradeReturn(new SGetSavvy());
            return;
        }

        /* internal  */onSavvyRateChangeHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SSavvyRate;
            AcupointData.setSavvyRate(loc1);
            if (this._savvyModule == null) 
            {
                return;
            }
            this.savvyModule.updateRate(loc1.rate, loc1.direction);
            return;
        }

        /* internal  */onAcupointInfoReciveHandler(arg1: MessageBlock): void
        {
            var loc2=0;
            var loc1=arg1.messageBase as SAcupointInfo;
            AcupointData.setInfo(loc1);
            if (!(this._savvyModule == null) && !this._savvyModule.isHide) 
            {
                loc2 = loc1.step;
                if (loc2 > 1 && loc2 <= 8) 
                {
                    --loc2;
                }
                else if (loc2 == 1) 
                {
                    loc2 = 8;
                }
                this.savvyModule.resetConfigInfo(loc2);
            }
            return;
        }

        private /* var */_savvyModule: IPlayerRootSavvyModule;
    }
